package main;
import java.util.ArrayList;

/**
 * will contain an array of units. Each player will have one.
 * @author 12817152
 *
 */
public class UnitArray {

	private ArrayList <Unit> Units;
	private Player Owner;
	private double milMult=1;//bonus attack earned through political system
	private int unitsKilled=0; //the number of killed by the opposing team.
	

/**
 * basic constructor for unit array
 * @param o Player owner
 */
	public UnitArray(Player o){
		Units= new ArrayList<Unit>();
		Owner=o;
	}
	/**
	 * get a unit from the array
	 * @param index of unit
	 * @return the unit
	 */
	public Unit getUnit(int index){
		if(index-1<= Units.size())
			return Units.get(index);
		else 
			return null;
	}
	/**
	 * add a unit to the array
	 * @param U unit
	 */
	public void addUnit (Unit U){
		Units.add(U);
			}
	
	/**
	 * checks all members of array to see if they contain a certain location
	 * @param xpos
	 * @param ypos
	 * @return
	 */
	public boolean checkLocation(int xpos, int ypos){
		if(Units.size()==0)
			return true;
		for(int i=0; i< Units.size();i++){
			int [] location=Units.get(i).getLocation();
			if(location[0]==xpos && location[1]==ypos){
				return false;
			}
		}
		return true;
	}
	/**
	 * increment turn method shared by everything and then some
	 */
	public void incrementTurn(){
		if(Owner.getPoliticalSystem().getSoc()<4) //implemented succesfully
			milMult+=.04;
		for(Unit A: Units){
			A.incrementTurn();
		}
	}
	
	public ArrayList<Unit> getArray(){
		return Units;
	}
	/**
	 * method to kill a unit from combat. seperate from manual removal externally
	 * @param A
	 */
	public void killUnit(Unit A){
		Units.remove(A);
		unitsKilled++;
	}
	/**
	 * upgrade all LIVING units int the array
	 * @param name
	 * @param target
	 * @param improve
	 */
	public void upgrade(String name, String target, int improve){
		for(Unit A: Units){
			if(A.getName().equals(name)|| name.equals("All")){
				if(target.equals("Dam"))
					A.setDamage(A.getDamage()+improve);
				if(target.equals("Health")){
					A.setmHealth(A.getmHealth() + improve);
					A.heal();
				}
				if(target.equals("Move"))
					A.setMove(A.getMove()+improve);
			}
		}
	}
	/**
	 * clear out anything that managed to survive through bugs. called every turn
	 */
	public void removeDead() {
		ArrayList<Unit> deadUnits = new ArrayList<Unit>();
		
		for(Unit A: Units){
			if(!A.isAlive()){
				deadUnits.add(A);
				unitsKilled++;
			}
		}
		
		for(Unit A: deadUnits) {
			Units.remove(A);
		}
		
	}
	/**
	 * @return the milMult
	 */
	public double getMilMult() {
		return milMult;
	}

	/**
	 * @param milMult the milMult to set
	 */
	public void setMilMult(double milMult) {
		this.milMult = milMult;
	}
	/**
	 * @return the unitsKilled
	 */
	public int getUnitsKilled() {
		return unitsKilled;
	}

	/**
	 * @param unitsKilled the unitsKilled to set
	 */
	public void setUnitsKilled(int unitsKilled) {
		this.unitsKilled = unitsKilled;
	}
}
